package com.gray.blog.common.annotation.validated;

import com.gray.blog.common.annotation.ArgEnumCheckTrigger;
import java.util.Arrays;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import lombok.extern.slf4j.Slf4j;

/**
 * Created  By zhoux 2019/03/11
 **/
@Slf4j
public class ArgEnumCheckValidator implements ConstraintValidator<ArgEnumCheckTrigger, Object>{

    private String[] checkList;

    @Override
    public void initialize(ArgEnumCheckTrigger constraintAnnotation) {
        checkList = constraintAnnotation.enumGroups();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        return checkList == null || checkList.length <= 0 || Arrays.stream(checkList).anyMatch(data -> data.equals(value.toString()));
    }

}
